package Game;
//2023/04/25
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static javax.swing.JOptionPane.showOptionDialog;


public class JframeWin {
    static JButton[] jbs=new JButton[25];
    static int kk=0;
    static int Cheat=0;//金手指 外挂
   static GameMusic music=new GameMusic();//实例化音乐类

    //定义全部变量存储按钮图片地址
    static String str="";
    public static void main(String[] args) {

        JFrame jf=new JFrame();//引入窗口工具
        jf.setSize(500,600);//设置尺寸
        jf.setLocationRelativeTo(null);//设置居中
        jf.setTitle("我的游戏");//设置标题
        jf.setResizable(false);//固定大小 无法最大化
        jf.setDefaultCloseOperation(3);//设置关闭程序
        jf.setAlwaysOnTop(false);//窗口置顶
        music.playBgSound("Music/03.mp3");
        //创建两个面板
        JPanel jpUp=new JPanel();
        JPanel JpDown=new JPanel();
        addmMenu(jf);//设置菜单栏
        //设置面板布局
        jpUp.setLayout(new GridLayout(5,5));

        //创建重置按钮
        JButton jbre= new JButton();

        jbre.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                music.playBgSound(1);
            //设置提示框
            int reply=JOptionPane.showConfirmDialog(null,"确定要重置游戏吗？","重置",0,2);
            //int reply = JOptionPane.showConfirmDialog(null, "确定要进行这个操作吗？", "确认", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

            if (reply==0) {
                if(Cheat!=0/*判断是否开启外挂*/){
                   Cheat=0;/*重设外挂*/
                }
                music.playBgSound("Music/03.mp3");
                kk=0;
                for (int i = 0; i <jbs.length; i++) {
                    jbs[i].setIcon(new ImageIcon("src/img/b.png"));
                }
            }else{
                music.playBgSound("Music/03.mp3");
            }
            }
        });
        jbre.setText("重置");

        //将重置按钮添加至面板布局
        JpDown.add(jbre);

        //循环->添加按钮
        for (int i = 0; i < jbs.length; i++) {
            //创建按钮
            jbs[i]=new JButton(new ImageIcon("src/img/b.png"));
            //路径地址
            str=jbs[i].getIcon().toString();
            //存储下标索引
            jbs[i].setActionCommand(String.valueOf(i));

            //设置按钮点击事件
            jbs[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //设置事件
                    int num=Integer.parseInt(e.getActionCommand());
                    //横排
                    if((num+1)%5==0){
                        change(jbs[num]);
                        change(jbs[num-1]);
                     }else{
                        change(jbs[num]);
                        change(jbs[num+1]);
                        if(num-1>=0 && (num%5!=0)){change(jbs[num-1]);}
                    }

                    //纵排
                    if(num+5<=24){change(jbs[num+5]);}
                    if(num-5>=0){change(jbs[num-5]);}
                    win();
                    ;
                }
            });

            //把25个图片添加到面板中去
            jpUp.add(jbs[i]);
            //添加面板到窗口
            jf.add("Center",jpUp);
        }
        jf.add("South",JpDown);
        jf.setVisible(true);//设置可视

//        JMenuBar jM=new JMenuBar();
//        JMenu F1=new JMenu("一级菜单");
//        JMenu F2=new JMenu("二级菜单");
//
//        JMenuItem jL1=new JMenuItem("功能1");
//        JMenuItem jL2=new JMenuItem("功能2");
    }

    /**
     * 定义按钮事件 图片切换
     * @param jb
     */
    public static void change(JButton jb){
        if(kk!=2) {
            //判断按钮图片地址是哪个
            music.playSound("Music/01.mp3");
            if (str.equals(jb.getIcon().toString())) {
                jb.setIcon(new ImageIcon("src/img/a.png"));
            } else {
                if(Cheat==1){
                    jb.setIcon(new ImageIcon("src/img/a.png"));//b
                }else {
                     jb.setIcon(new ImageIcon("src/img/b.png"));//b
                }

            }
        }
    }

    /**
     * 判断输赢！
     */
    public static void win(){
        int count=0;
        for (int i = 0; i < jbs.length; i++) {
            if(jbs[i].getIcon().toString().equals("src/img/a.png")){
                count++;
            }
        }

        if(count==25){
            kk=2;
            music.playBgSound(1);
            music.playWinSound("Music/05.mp3");
            for (int i = 0; i < jbs.length; i++) {
                jbs[i].setIcon(new ImageIcon("src/img/c.png"));
            }
        }
    }


    public static void addmMenu(JFrame Window){
        //实例化一个菜单栏
        JMenuBar jMenuBar=new JMenuBar();

        JMenu listNo1=new JMenu("设置");
        listNo1.add("开挂").addActionListener(e-> {
            String[] arr = {"开启", "关闭"};
             music.playBgSound(1);
            int re = showOptionDialog(
                    null, "开启外挂后，卡牌将不会反转,是否开启？",
                    "提示", 0, 1, null,arr, arr[1]);

            if(re==0){
                 music.playBgSound("Music/02.mp3");
                Cheat=1;
                JOptionPane.showMessageDialog(null,"外挂已开启！","提示",-1);
            } else if (re==1) {
                music.playBgSound("Music/02.mp3");
                Cheat=0;
            }

        });
        JMenu listNo2=new JMenu("关于");

        jMenuBar.add(listNo1);
        jMenuBar.add(listNo2);
        Window.setJMenuBar(jMenuBar);


    }
}
